﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class src_profilo : webBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.checkLogin();
        display();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String old = oldPass.Text;
        String newpass = newPass.Text;
        String confpass = confPass.Text;

        if (newpass == confpass)
        {
            if (DB.selectQuery("ForumDB", "select count(*) from userForum where username='" + utente.username + "' and password='" + old + "'")[0][0]=='1')
            {
                utente.newPwd(newpass);
                Ris.Text = "Password modificata con successo";
            }
            else 
            {
                Ris.Text = "Vecchia password non corretta";
            }
        }
        else
            Ris.Text = "Le password NON sono uguali";


    }
    protected void Bupload_Click(object sender, EventArgs e)
    {
        if(fileupload1.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fileupload1.FileName);
                fileupload1.SaveAs(Server.MapPath("~/img/profili/") + filename);
                utente.img = "/FORUMDEFINITIVO/img/profili/" + filename;
            }

            catch(Exception exc)
            {
                throw;
            }
        }
    }
    protected void Bsend_Click(object sender, EventArgs e)
    {
        utente.name = TBnome.Text;
        utente.surname = TBcognome.Text;
        utente.email = TBEmail.Text;
        utente.update();
        display();
    }
    protected void BEdit_Click(object sender, EventArgs e)
    {
        Lbdisable();
        TBenable();
        BEdit.Visible = false;
        Bsend.Visible = true;

    }
    private void display() {
        TBdisable();
        Lbenable();
        Bsend.Visible = false;
        BEdit.Visible = true;
        LBnome.Text = utente.name;
        LBcognome.Text = utente.surname;
        LBEmail.Text = utente.email;
    }
    private void TBdisable()
    {
        TBnome.Visible = false;
        TBcognome.Visible = false;
        TBEmail.Visible = false;
    }
    private void TBenable()
    {
        TBnome.Visible = true;
        TBcognome.Visible = true;
        TBEmail.Visible = true;

        TBnome.Text = LBnome.Text;
        TBcognome.Text = LBcognome.Text;
        TBEmail.Text = LBEmail.Text;
    }
    private void Lbdisable() {
        LBnome.Visible = false;
        LBcognome.Visible = false;
        LBEmail.Visible = false;
    }
    private void Lbenable()
    {
        LBnome.Visible = true;
        LBcognome.Visible = true;
        LBEmail.Visible = true;
    }
}